package com.starview.star.compiler.transform;

import com.starview.star.compiler.canonical.DefaultTransformer;
import com.starview.star.compiler.canonical.FunctionLiteral;
import com.starview.star.compiler.canonical.IContentAction;
import com.starview.star.compiler.canonical.IContentExpression;
import com.starview.star.compiler.canonical.IStatement;
import com.starview.star.compiler.canonical.LetAction;
import com.starview.star.compiler.canonical.LetTerm;
import com.starview.star.compiler.canonical.PatternAbstraction;
import com.starview.star.compiler.canonical.VarEntry;
import com.starview.star.compiler.canonical.Variable;

public class Inliner extends DefaultTransformer<InlineContext>
{

  @Override
  public IContentExpression transformFunctionLiteral(FunctionLiteral f, InlineContext context)
  {
    // TODO Auto-generated method stub
    return super.transformFunctionLiteral(f, context);
  }

  @Override
  public IContentExpression transformLetTerm(LetTerm let, InlineContext context)
  {
    InlineContext sub = context.fork();
    for (IStatement stmt : let.getEnvironment()) {
      if (stmt instanceof VarEntry)
        sub.exclude(stmt.definedFields());
    }
    return super.transformLetTerm(let, sub);
  }

  @Override
  public IContentExpression transformPatternAbstraction(PatternAbstraction pattern, InlineContext context)
  {
    // TODO Auto-generated method stub
    return super.transformPatternAbstraction(pattern, context);
  }

  @Override
  public IContentExpression transformVariable(Variable var, InlineContext context)
  {
    return context.replaceVar(var);
  }

  @Override
  public IContentAction transformLetAction(LetAction let, InlineContext context)
  {
    InlineContext sub = context.fork();
    for (IStatement stmt : let.getEnvironment()) {
      if (stmt instanceof VarEntry)
        sub.exclude(stmt.definedFields());
    }

    return super.transformLetAction(let, sub);
  }
}
